import easygui as eg
from database import Database
from user_management import show_user_management
from guide_management import show_guide_management
from tourist_management import show_tourist_management
from route_management import show_route_management
from group_management import show_group_management


def show_guide_info(db, param):
    pass


def show_tourist_info(db, param):
    pass


def show_my_groups(db, user):
    pass


def show_my_tours(db, user):
    pass


def show_routes(db, user):
    pass


def show_main_app(db, user):
    user_type = user['UserType']
    username = user['Username']
    realname = user['RealName'] if user['RealName'] else username

    # 根据用户类型确定可用功能
    if user_type == 'admin':
        menu_choices = [
            "用户管理",
            "导游管理",
            "游客管理",
            "旅游线路管理",
            "旅游团管理",
            "修改密码",
            "退出"
        ]
    elif user_type == 'guide':
        menu_choices = [
            "我的信息",
            "我的旅游团",
            "旅游线路",
            "修改密码",
            "退出"
        ]
    else:  # tourist
        menu_choices = [
            "我的信息",
            "我的旅游团",
            "旅游线路",
            "修改密码",
            "退出"
        ]

    while True:
        # 显示主菜单
        title = f"旅行社管理系统 - {realname} ({get_user_type_name(user_type)})"
        choice = eg.choicebox("请选择操作", title, menu_choices)

        if choice is None or choice == "退出":
            break

        # 处理选择
        if choice == "用户管理":
            show_user_management(db, user)
        elif choice == "导游管理":
            show_guide_management(db, user)
        elif choice == "游客管理":
            show_tourist_management(db, user)
        elif choice == "旅游线路管理":
            show_route_management(db, user)
        elif choice == "旅游团管理":
            show_group_management(db, user)
        elif choice == "我的信息":
            if user_type == 'guide':
                show_guide_info(db, user['UserID'])
            else:
                show_tourist_info(db, user['UserID'])
        elif choice == "我的旅游团":
            if user_type == 'guide':
                show_my_groups(db, user)
            else:
                show_my_tours(db, user)
        elif choice == "旅游线路":
            show_routes(db, user)
        elif choice == "修改密码":
            change_password(db, user['UserID'])


def get_user_type_name(user_type):
    types = {'admin': '管理员', 'guide': '导游', 'tourist': '游客'}
    return types.get(user_type, '未知')


def change_password(db, user_id):
    while True:
        fields = ["原密码", "新密码", "确认新密码"]
        values = eg.multpasswordbox("修改密码", "密码修改", fields)

        if values is None:  # 用户点击取消
            break

        old_password, new_password, confirm_password = values

        if not old_password or not new_password or not confirm_password:
            eg.msgbox("请填写所有密码字段", "错误")
            continue

        if new_password != confirm_password:
            eg.msgbox("新密码与确认密码不匹配", "错误")
            continue

        # 验证原密码
        hashed_old = Database.hash_password(old_password)
        query = "SELECT Password FROM Users WHERE UserID = ?"
        result = db.execute_query(query, (user_id,))

        if not result or result[0]['Password'] != hashed_old:
            eg.msgbox("原密码不正确", "错误")
            continue

        # 更新密码
        hashed_new = Database.hash_password(new_password)
        update_query = "UPDATE Users SET Password = ? WHERE UserID = ?"
        affected = db.execute_query(update_query, (hashed_new, user_id), False)

        if affected == 1:
            eg.msgbox("密码修改成功", "成功")
            break
        else:
            eg.msgbox("密码修改失败", "错误")